package com.CGFrame.struts.bean;

//import org.apache.struts.util.LabelValueBean;
import org.hibernate.*;

import com.CGFrame.struts.POJOs.*;

import java.util.*;

//Authority
//Category
//Company
//Sector
//Employee
//Reguser
//Project
//ElementGroup
//Element
//Shotgroup
//Shot
//Make
//Task
//Confirm
//Role
//
	

public class ProjectOperate{
//	public static HibernateSessionFactory hibernatesessionFactory;
	private  Session session = null;
	public ProjectOperate()
	{
		session = HibernateSessionFactory.getSession();
		
	}
	public void close()
	{
		HibernateSessionFactory.closeSession();
	}
	public void saveObject(Object ob)  
	{
//		session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		try 
		{
			tx = session.beginTransaction();
			session.saveOrUpdate(ob);
			tx.commit();
		} 
		catch (Exception e) 
		{
			if (tx != null) 
			{
				tx.rollback();
			}
			System.out.println("ProjectOperate2:"+ e.toString());
		}
	}
	public void deleteObject(String Type,int ob_id)
	{
//		session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		try 
		{
			tx = session.beginTransaction();
			if(Type.equals("Company"))
				session.delete((Company)session.load(Company.class, ob_id));
			else if(Type.equals("Sector"))
				session.delete((Sector)session.load(Sector.class, ob_id));
			else if(Type.equals("Reguserrole"))
				session.delete((Reguserrole)session.load(Reguserrole.class, ob_id));
			else if(Type.equals("Reguser"))
			{
				Reguser reguser = (Reguser) session.load(Reguser.class, ob_id);
				Status status = (Status)this.UniqueQuery("Status", "statusType,DELETE");
				reguser.setStatus(status);
				session.update(reguser);
			}
//				session.delete((Reguser)session.load(Reguser.class, ob_id));
			else if(Type.equals("Role"))
				session.delete((Role)session.load(Role.class, ob_id));
			else if(Type.equals("Employee"))
			{				
				Employee employee = (Employee) session.load(Employee.class, ob_id);
				Status status = (Status)this.UniqueQuery("Status", "statusType,DELETE");
				employee.setStatus(status);
				//// ����Ա��������ע���û�Ҳɾ��.
				Iterator ite_RegUsers = employee.getRegusers().iterator();
				while(ite_RegUsers.hasNext())
				{
					Reguser reguser = (Reguser)ite_RegUsers.next();
					reguser.setStatus(status);
				}
				
				session.update(employee);
			}
			else if(Type.equals("Makerelation"))
				session.delete((Makerelation)session.load(Makerelation.class, ob_id));
			else if(Type.equals("Category"))//Category
				session.delete((Category)session.load(Category.class, ob_id));
			else if(Type.equals("Catemodel"))//Model
				session.delete((Catemodel)session.load(Catemodel.class, ob_id));
			else if(Type.equals("Catemodelrel"))//Catemodelrel
				session.delete((Catemodelrel)session.load(Catemodelrel.class, ob_id));
			else if(Type.equals("Catemodeltime"))//Catemodeltime
				session.delete((Catemodeltime)session.load(Catemodeltime.class, ob_id));
			else if(Type.equals("Message"))//Message
				session.delete((Message)session.load(Message.class, ob_id));
			/*else if(Type.equals("Authority"))
			session.delete((Authority)session.load(Authority.class, ob_id));
			else if(Type.equals("Sector"))//Category
			session.delete((Sector)session.load(Sector.class, ob_id));
			else if(Type.equals("Reguser"))
			session.delete((Reguser)session.load(Reguser.class, ob_id));
			else if(Type.equals("Project"))
			session.delete((Project)session.load(Project.class, ob_id));
			else if(Type.equals("ElementGroup"))
			session.delete((ElementGroup)session.load(ElementGroup.class, ob_id));
			else if(Type.equals("Element"))
			session.delete((Element)session.load(Element.class, ob_id));
			else if(Type.equals("Shotgroup"))
			session.delete((Shotgroup)session.load(Shotgroup.class, ob_id));
			else if(Type.equals("Shot"))
			session.delete((Shot)session.load(Shot.class, ob_id));
			else if(Type.equals("Make"))
			session.delete((Make)session.load(Make.class, ob_id));
			else if(Type.equals("Task"))
			session.delete((Task)session.load(Task.class, ob_id));
			else if(Type.equals("Confirm"))
			session.delete((Confirm)session.load(Confirm.class, ob_id));
			else if(Type.equals("Role"))
			session.delete((Role)session.load(Role.class, ob_id));
			else
			{
				System.out.println("Wrong Type");
				return;
			}*/
			// We're done; make our changes permanent
			tx.commit();

		} 
		catch (Exception e) 
		{
			if (tx != null) 
			{
				// Something went wrong; discard all partial changes
				tx.rollback();
			}
			System.out.println("ProjectOperate3:"+ e.toString());
		} 
		
	}
	public Object load(String Type,int ob_id)
	{
//		session = HibernateSessionFactory.getSession();
		Object c = null;
		try
		{
			if(Type.equals("Company"))			
			{
				c = (Company) session.load(Company.class, ob_id);
				return c;
			}
			else if(Type.equals("Class"))
			{
				c = (Sort) session.load(Sort.class, ob_id);
				return c;
			}
			else if(Type.equals("Sector"))
			{
				c = (Sector) session.load(Sector.class, ob_id);
				return c;
			}
			else if(Type.equals("Makerelation"))
			{
				c = (Makerelation) session.load(Makerelation.class, ob_id);
				return c;
			}
			else if(Type.equals("Category")){
				c = (Category) session.load(Category.class, ob_id);
				return c;
			}
			
		else if(Type.equals("Authority"))
		 c =  session.load(Authority.class, ob_id);
		else if(Type.equals("Sector"))
		 c =  session.load(Sector.class, ob_id);
		else if(Type.equals("Employee"))
		 c =  session.load(Employee.class, ob_id);
		else if(Type.equals("Reguser"))
		 c =  session.load(Reguser.class, ob_id);
		else if(Type.equals("Project"))
		 c =  session.load(Project.class, ob_id);
		else if(Type.equals("Elementgroup"))
		 c =  session.load(Elementgroup.class, ob_id);
		else if(Type.equals("Element"))
		 c =  session.load(Element.class, ob_id);
		else if(Type.equals("Shotgroup"))
		 c =  session.load(Shotgroup.class, ob_id);
		else if(Type.equals("Shot"))
		 c =  session.load(Shot.class, ob_id);
		else if(Type.equals("Make"))
		 c = session.load(Make.class, ob_id);
		else if(Type.equals("Task"))
		 c =  session.load(Task.class, ob_id);
		else if(Type.equals("Confirm"))
		 c =  session.load(Confirm.class, ob_id);
		else if(Type.equals("Role"))
		 c =  session.load(Role.class, ob_id);
		
		}
		catch (Exception e) 
		{
			
			System.out.println("ProjectOperate4:"+ e.toString());
		} 
		return c;
		
	}
	public List Query(String ClassName,String HQL,String OrderBy,String AscOrDesc)
	{
//		session = HibernateSessionFactory.getSession();
		String QueryStr=QueryGenerator(ClassName,HQL,OrderBy,AscOrDesc);
		session.enableFilter("statusFilter").setParameter("type",100);
		
		return session.createQuery(QueryStr).list();
	}
	public List Query(String hql) {
		
		session.enableFilter("statusFilter").setParameter("type",100);	
		return  session.createQuery(hql).list();
	}
	public Query getQuery(String hql){
		session.enableFilter("statusFilter").setParameter("type",100);
		return session.createQuery(hql);
	}
	public Object UniqueQuery(String ClassName,String HQL)
	{
//		session = HibernateSessionFactory.getSession();
		String QueryStr=QueryGenerator(ClassName,HQL,null,null);
	
		session.enableFilter("statusFilter").setParameter("type", 100);
		return session.createQuery(QueryStr).uniqueResult();
	}
	
	public String QueryGenerator(String ClassName,String HQL,String OrderBy,String AscOrDesc)
	{
		String QueryString = " from "+ClassName+" as o ";
		if(HQL!=null)
		{
			QueryString=QueryString+"where ";
			String [] Q = HQL.split(",");			
			for(int i=0;i<Q.length;i++)
			{
				if(i==0)
				{
					QueryString=QueryString+"o."+Q[i]+"=";
				}
				else if(i%2==0)
				{
					QueryString=QueryString+" and o."+Q[i]+"=";
				}
				else
				{
					QueryString=QueryString+"'"+Q[i]+"'";
				}
				
			}
		}
		if(OrderBy!=null)
		{
			QueryString=QueryString+" order by o."+OrderBy+" "+AscOrDesc;
		}
		return QueryString;
	}
	public List QueryColumn(String Column , String ClassName ,String HQL, String OrderBy,String AscOrDesc)
	{
		String QueryString = "Select distinct o."+Column+QueryGenerator(ClassName,HQL,OrderBy,AscOrDesc);
		return session.createQuery(QueryString).list();
	}
	public Object QueryMax(String Column , String ClassName ,String HQL, String OrderBy,String AscOrDesc)
	{
		String QueryString = "Select max( o."+Column+")"+QueryGenerator(ClassName,HQL,OrderBy,AscOrDesc);
		return session.createQuery(QueryString).uniqueResult();
	}
	public int[] Distinct (int [] array)
	{
		
		String temp="";
		for(int i=0;i<array.length;i++)
		{
			for(int j=i+1;j<array.length;j++)
			{
				if(array[i]==array[j])
				{
					array[j]=-1;
				}
			}		
		}
		for(int k=0;k<array.length;k++)
		{
			if(k==0)
			{
				temp+=array[k]+"";
			}
			else if(array[k]!=-1)
			{
				temp+=","+array[k]+"";
			}
		}
		String [] tempArray = temp.split(",");
		int result[] = new int [tempArray.length];
		for(int l=0;l<tempArray.length;l++)
		{
			result[l]=Integer.parseInt(tempArray[l]);
		}
		return result;
		
	}
	public List ListObject(String type,int ids [])
	{
		List objects =null;
		for (int i=0;i<ids.length;i++)
		{
			objects.add(load(type,ids[i]));
		}
		return objects;
	}
	/*
	public Vector Navigator (int authorityId)
	{
		Vector v = new Vector();
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Authority a =  (Authority)pop.UniqueQuery("Authority", "authorityId,"+authorityId);
		v.add(new LabelValueBean(a.getSort().getGuide().getGuideName(),a.getSort().getGuide().getGuideId()+""));
		v.add(new LabelValueBean(a.getSort().getSortName(),a.getSort().getSortId()+""));
		v.add(new LabelValueBean(a.getAuthorityName(),authorityId+""));
		return v;
	}
	*/

	
	
	
	public List PageQuery(String ClassName,String HQL,String OrderBy,String AscOrDesc,int curPage,int ItemNo)
	{
		String QueryStr=QueryGenerator(ClassName,HQL,OrderBy,AscOrDesc);
		session.enableFilter("statusFilter").setParameter("type", 100);
		Query query = session.createQuery(QueryStr);
		query.setFirstResult((curPage-1)*ItemNo);
		query.setMaxResults(ItemNo);
		return query.list();
	}
	public int CountQuery(String ClassName,String HQL)
	{
		
		String QueryStr=QueryGenerator(ClassName,HQL,null,null);
		List list = session.createQuery(QueryStr).list();	
		return list.size();
	}
	
	public void delete(Object ob)
	{
//		session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		try 
		{
			tx = session.beginTransaction();
			session.delete(ob);
			tx.commit();
		}
		catch (Exception e) 
		{
			
			System.out.println("ProjectOperate4:"+ e.toString());
		} 
	}

	
	
}